<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>在线考试系统 - 学生主页</title>
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/css/all.min.css">
    <style>
        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --student-accent: #4361ee;
            --card-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            --transition: all 0.3s ease;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f7fb;
            color: #333;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        /* 导航栏样式 */
        .navbar {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 0.8rem 1rem;
        }

        .navbar-brand {
            font-weight: 700;
            font-size: 1.5rem;
            display: flex;
            align-items: center;
        }

        .navbar-brand i {
            margin-right: 10px;
            font-size: 1.8rem;
        }

        .nav-link {
            color: rgba(255, 255, 255, 0.85) !important;
            font-weight: 500;
            padding: 0.5rem 1rem !important;
            border-radius: 4px;
            margin: 0 3px;
            transition: var(--transition);
        }

        .nav-link:hover, .nav-link.active {
            background-color: rgba(255, 255, 255, 0.15);
            color: white !important;
        }

        /* 主内容区域 */
        .main-content {
            flex: 1;
            padding: 2rem 0;
        }

        .welcome-section {
            background: white;
            border-radius: 15px;
            padding: 1.5rem;
            box-shadow: var(--card-shadow);
            margin-bottom: 2rem;
            position: relative;
            overflow: hidden;
        }

        .welcome-section::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 5px;
            height: 100%;
            background-color: var(--student-accent);
        }

        .welcome-title {
            font-weight: 700;
            margin-bottom: 0.5rem;
        }

        .welcome-text {
            color: #6c757d;
            margin-bottom: 0;
        }

        /* 功能卡片 */
        .card {
            border: none;
            border-radius: 12px;
            box-shadow: var(--card-shadow);
            transition: var(--transition);
            height: 100%;
            overflow: hidden;
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
        }

        .card-header {
            background: white;
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
            padding: 1.25rem 1.5rem;
            font-weight: 700;
            display: flex;
            align-items: center;
        }

        .card-header i {
            margin-right: 10px;
            font-size: 1.25rem;
            color: var(--student-accent);
        }

        .card-body {
            padding: 1.5rem;
        }

        .card-title {
            font-weight: 700;
            margin-bottom: 0.75rem;
        }

        .card-text {
            color: #6c757d;
            margin-bottom: 1.5rem;
        }

        .btn-primary {
            background: linear-gradient(135deg, var(--student-accent), #3a0ca3);
            border: none;
            padding: 0.5rem 1.5rem;
            font-weight: 600;
            border-radius: 8px;
            transition: var(--transition);
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(67, 97, 238, 0.4);
        }

        /* 统计数据样式 */
        .stat-number {
            font-size: 2.2rem;
            font-weight: 700;
            line-height: 1;
            color: var(--student-accent);
        }

        /* 页脚样式 */
        .footer {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            color: white;
            padding: 2rem 0;
            margin-top: auto;
        }

        .student .footer {
            background: linear-gradient(135deg, var(--student-accent), #3a0ca3);
        }

        .footer-title {
            font-weight: 700;
            margin-bottom: 1rem;
            font-size: 1.25rem;
        }

        .footer-links {
            list-style: none;
            padding: 0;
        }

        .footer-links li {
            margin-bottom: 0.5rem;
        }

        .footer-links a {
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            transition: var(--transition);
        }

        .footer-links a:hover {
            color: white;
            text-decoration: underline;
        }

        .social-icons {
            display: flex;
            gap: 15px;
            margin-top: 1rem;
        }

        .social-icons a {
            color: white;
            font-size: 1.5rem;
            transition: var(--transition);
        }

        .social-icons a:hover {
            transform: translateY(-3px);
        }

        .copyright {
            text-align: center;
            padding-top: 1.5rem;
            margin-top: 1.5rem;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
            color: rgba(255, 255, 255, 0.7);
            font-size: 0.9rem;
        }

        /* 响应式调整 */
        @media (max-width: 768px) {
            .navbar-brand {
                font-size: 1.2rem;
            }

            .welcome-section {
                padding: 1rem;
            }

            .stat-number {
                font-size: 1.8rem;
            }
        }
    </style>
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar navbar-expand-lg navbar-dark">
    <div class="container">
        <a class="navbar-brand" href="#">
            <i class="fas fa-graduation-cap"></i>在线考试系统
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav me-auto">
                <li class="nav-item">
                    <a class="nav-link active" href="/home/manage"><i class="fas fa-home"></i> 主页</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/student/exam/list"><i class="fas fa-pen-alt"></i> 考试信息</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/student/score/analysis"><i class="fas fa-chart-line"></i> 成绩分析</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/student/resources"><i class="fas fa-book-open"></i> 学习资源</a>
                </li>
            </ul>

            <ul class="navbar-nav ms-auto">
                <li class="nav-item">
                    <a class="nav-link" href="/student/profile/manage">
                        <i class="fas fa-user-circle"></i> 个人信息
                    </a>
                </li>

                <li class="nav-item">
                    <a class="nav-link" href="/logout">
                        <i class="fas fa-sign-out-alt"></i> 退出登录
                    </a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<!-- 主内容区域 -->
<div class="main-content">
    <div class="container">
        <div class="welcome-section">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h2 class="welcome-title">欢迎回来，<span id="studentName" th:text="${homeData != null ? homeData.realName : '同学'}">同学</span></h2>
                    <p class="welcome-text">您已累计参加 <span id="totalExams" th:text="${homeData != null ? homeData.totalExams : 0}">0</span> 场考试</p>
                </div>
                <div class="text-center">
                    <div class="stat-number" id="upcomingExams" th:text="${homeData != null ? homeData.upcomingExams : 0}">0</div>
                    <small>待参加考试</small>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-lg-4 col-md-6 mb-4">
                <div class="card">
                    <div class="card-header">
                        <i class="fas fa-pen-alt"></i> 参与考试
                    </div>
                    <div class="card-body">
                        <h5 class="card-title">待参加考试</h5>
                        <p class="card-text">您有 <span class="fw-bold" id="examsCount" th:text="${homeData != null ? homeData.examsCount : 0}">0</span> 场待参加的考试</p>
                        <p class="text-muted small" id="exam-next-info" th:if="${homeData != null and homeData.upcomingExams > 0}" th:text="'您有 ' + ${homeData.upcomingExams} + ' 场考试待参加'">暂无待参加考试</p>
                        <p class="text-muted small" id="exam-next-info-empty" th:if="${homeData == null or homeData.upcomingExams == 0}">暂无待参加考试</p>
                        <a href="/student/exam/list" class="btn btn-primary">查看考试</a>
                    </div>
                </div>
            </div>

            <div class="col-lg-4 col-md-6 mb-4">
                <div class="card">
                    <div class="card-header">
                        <i class="fas fa-chart-line"></i> 成绩查询
                    </div>
                    <div class="card-body">
                        <h5 class="card-title">考试成绩分析</h5>
                        <p class="card-text">查看您最近的考试成绩和排名情况</p>
                        <!-- 最近考试信息 -->
                        <div th:if="${homeData != null and homeData.lastExamName != null}" class="text-muted small">
                            <p class="mb-1">
                                <strong>最近考试：</strong>
                                <span th:text="${homeData.lastExamName}"></span>
                            </p>
                            <p class="mb-1">
                                <strong>课程：</strong>
                                <span th:text="${homeData.lastExamCourse}"></span>
                            </p>
                            <p class="mb-1" th:if="${homeData.lastExamScore != null and homeData.lastExamTotalScore != null}">
                                <strong>成绩：</strong>
                                <span th:text="${homeData.lastExamScore + '/' + homeData.lastExamTotalScore}"></span>
                            </p>
                            <p class="mb-1" th:if="${homeData.lastExamTime != null}">
                                <strong>时间：</strong>
                                <span th:text="${#temporals.format(homeData.lastExamTime, 'yyyy-MM-dd HH:mm')}"></span>
                            </p>
                        </div>
                        <p class="text-muted small" id="last-exam-info" th:if="${homeData != null and homeData.totalExams > 0 and homeData.lastExamName == null}" th:text="'您已参加 ' + ${homeData.totalExams} + ' 场考试'">暂无考试记录</p>
                        <p class="text-muted small" id="last-exam-info-empty" th:if="${homeData == null or homeData.totalExams == 0}">暂无考试记录</p>
                        <a href="/student/score/analysis" class="btn btn-primary">查看成绩</a>
                    </div>
                </div>
            </div>

            <div class="col-lg-4 col-md-6 mb-4">
                <div class="card">
                    <div class="card-header">
                        <i class="fas fa-book-open"></i> 学习资源
                    </div>
                    <div class="card-body">
                        <h5 class="card-title">课程资料库</h5>
                        <p class="card-text">访问学习资料、课件和复习材料</p>
                        <!-- 最近题库更新信息 -->
                        <div th:if="${homeData != null and homeData.latestQuestionCourse != null}" class="text-muted small">
                            <p class="mb-1">
                                <strong>最近更新：</strong>
                                <span th:text="${homeData.latestQuestionCourse}"></span>
                            </p>
                            <p class="mb-1" th:if="${homeData.latestQuestionType != null}">
                                <strong>题目类型：</strong>
                                <span th:text="${homeData.latestQuestionType}"></span>
                            </p>
                            <p class="mb-1" th:if="${homeData.latestQuestionTime != null}">
                                <strong>更新时间：</strong>
                                <span th:text="${#temporals.format(homeData.latestQuestionTime, 'yyyy-MM-dd HH:mm')}"></span>
                            </p>
                        </div>
                        <p class="text-muted small" id="latest-question-update" th:if="${homeData == null or homeData.latestQuestionCourse == null}">最近更新：高等数学第三章课件</p>
                        <a href="/student/resources" class="btn btn-primary">学习资源</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 页脚 -->
<footer class="footer">
    <div class="container text-center py-4">
        <a href="https://gitee.com/sorrymaker04/onlineexam-workload" target="_blank"><i class="fab fa-github fa-2x"></i></a>
        <div class="copyright mt-2">
            &copy; 2025 版权所有 北城在线考试系统小组 12 Team
        </div>
    </div>
</footer>
<!-- Bootstrap JS -->
<script src="/js/bootstrap.bundle.min.js"></script>

<script>
    // 页面加载完成后，如果没有后端数据，则通过AJAX获取
    document.addEventListener('DOMContentLoaded', function() {
        // 检查是否有后端数据
        const studentName = document.getElementById('studentName').textContent;
        const totalExams = document.getElementById('totalExams').textContent;
        
        // 如果数据是默认值，则通过AJAX获取
        if (studentName === '同学' && totalExams === '0') {
            loadHomeData();
        }
    });

    // 加载首页数据（AJAX备选方案）
    function loadHomeData() {
        fetch('/student/home/data')
            .then(response => {
                if (!response.ok) {
                    throw new Error('网络响应错误');
                }
                return response.json();
            })
            .then(data => {
                if (data) {
                    // 更新页面数据
                    document.getElementById('studentName').textContent = data.realName || '同学';
                    document.getElementById('totalExams').textContent = data.totalExams || 0;
                    document.getElementById('upcomingExams').textContent = data.upcomingExams || 0;
                    document.getElementById('examsCount').textContent = data.examsCount || 0;
                    
                    // 更新考试信息提示
                    const examNextInfo = document.getElementById('exam-next-info');
                    const examNextInfoEmpty = document.getElementById('exam-next-info-empty');
                    if (data.upcomingExams > 0) {
                        if (examNextInfo) {
                            examNextInfo.textContent = `您有 ${data.upcomingExams} 场考试待参加`;
                            examNextInfo.style.display = 'block';
                        }
                        if (examNextInfoEmpty) {
                            examNextInfoEmpty.style.display = 'none';
                        }
                    } else {
                        if (examNextInfo) {
                            examNextInfo.style.display = 'none';
                        }
                        if (examNextInfoEmpty) {
                            examNextInfoEmpty.style.display = 'block';
                        }
                    }
                    
                    // 更新成绩信息提示
                    const lastExamInfo = document.getElementById('last-exam-info');
                    const lastExamInfoEmpty = document.getElementById('last-exam-info-empty');
                    if (data.totalExams > 0) {
                        if (lastExamInfo) {
                            lastExamInfo.textContent = `您已参加 ${data.totalExams} 场考试`;
                            lastExamInfo.style.display = 'block';
                        }
                        if (lastExamInfoEmpty) {
                            lastExamInfoEmpty.style.display = 'none';
                        }
                    } else {
                        if (lastExamInfo) {
                            lastExamInfo.style.display = 'none';
                        }
                        if (lastExamInfoEmpty) {
                            lastExamInfoEmpty.style.display = 'block';
                        }
                    }
                    
                    // 更新最近考试信息（如果有）
                    if (data.lastExamName) {
                        updateLastExamInfo(data);
                    }
                    
                    // 更新题库更新信息（如果有）
                    if (data.latestQuestionCourse) {
                        updateLatestQuestionInfo(data);
                    }
                }
            })
            .catch(error => {
                console.error('加载数据失败:', error);
                // 保持默认值
            });
    }
    
    // 更新最近考试信息
    function updateLastExamInfo(data) {
        const cardBody = document.querySelector('.card:nth-child(2) .card-body');
        const existingInfo = cardBody.querySelector('.text-muted.small');
        
        if (existingInfo) {
            existingInfo.innerHTML = `
                <p class="mb-1">
                    <strong>最近考试：</strong>
                    <span>${data.lastExamName || ''}</span>
                </p>
                <p class="mb-1">
                    <strong>课程：</strong>
                    <span>${data.lastExamCourse || ''}</span>
                </p>
                ${data.lastExamScore && data.lastExamTotalScore ? 
                    `<p class="mb-1">
                        <strong>成绩：</strong>
                        <span>${data.lastExamScore}/${data.lastExamTotalScore}</span>
                    </p>` : ''
                }
                ${data.lastExamTime ? 
                    `<p class="mb-1">
                        <strong>时间：</strong>
                        <span>${new Date(data.lastExamTime).toLocaleString('zh-CN')}</span>
                    </p>` : ''
                }
            `;
        }
    }
    
    // 更新题库更新信息
    function updateLatestQuestionInfo(data) {
        const cardBody = document.querySelector('.card:nth-child(3) .card-body');
        const existingInfo = cardBody.querySelector('.text-muted.small');
        
        if (existingInfo) {
            existingInfo.innerHTML = `
                <p class="mb-1">
                    <strong>最近更新：</strong>
                    <span>${data.latestQuestionCourse || ''}</span>
                </p>
                ${data.latestQuestionType ? 
                    `<p class="mb-1">
                        <strong>题目类型：</strong>
                        <span>${data.latestQuestionType}</span>
                    </p>` : ''
                }
                ${data.latestQuestionTime ? 
                    `<p class="mb-1">
                        <strong>更新时间：</strong>
                        <span>${new Date(data.latestQuestionTime).toLocaleString('zh-CN')}</span>
                    </p>` : ''
                }
            `;
        }
    }
</script>
</body>
</html>